package br.usp.ime.mac.mico.factories;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class JPADAOFactory implements DAOFactory {
	//Factory of the underlying persitence used.
	private EntityManagerFactory factory;

	//Singleton
	private static JPADAOFactory instance = new JPADAOFactory(); 

	private JPADAOFactory() {
		factory = this.openConnection();
	}

	public static JPADAOFactory getInstance() {
		return instance;
	}

	public EntityManagerFactory getFactory() {
		return factory;
	}

	public EntityManagerFactory openConnection() {
		if (factory == null || !factory.isOpen()) {
			factory = Persistence.createEntityManagerFactory("MICO");
		}
		return factory;
	}

	public void closeConnection() {
		if (factory != null && factory.isOpen())
			factory.close();
	}

}